package MarrySum5;


import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * 需求：统计近二十年全国内地居民初婚数和再婚数
 * 分析：自定义一个对象DivorceCountBean,用于封装每年全国内地居民初婚数和再婚数，注意需要实现hadoop的序列化机制
 *      以全国national作为map阶段输出的key，作为value
 */
public class MarrySumMapper extends Mapper<LongWritable, Text,Text,MarryCountBean>{
    int i = 0;
    Text outKey = new Text();
    MarryCountBean outValue = new MarryCountBean();

    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, MarryCountBean>.Context context) throws IOException, InterruptedException {
        i++;
        if(key.get() == 0){
            return;
        }

        String[] filelds = value.toString().split(",");


        /**
         * 北京市，东城区,2022年,0.53,0.52,0.62,0.47,0.01,0.25,148,
         * 提取数据 年份、内地居民初婚数和再婚数
         */
        String year =filelds[2];
        outKey.set(year);
        //获取初婚数和再婚数
        System.out.println("1:"+i+"2:"+value.toString());
        float fmarry_num = Float.parseFloat(filelds[5]);
        float remarry_num = Float.parseFloat(filelds[6]);

        outValue.set(fmarry_num,remarry_num);

        /**
         * 输出结果
         */
        context.write(outKey,outValue);
    }
}
